package com.admin.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.admin.bean.MobileTypeBean;

@Repository("mobileTypeDao")
public class MobileTypeDao {

	@Autowired
	public JdbcTemplate jdbcTemplate;
	
	@Autowired
	public QueryRunner queryRunner;
	
	/**
	 * 添加新的手机型号
	 * @param type
	 * @return
	 */
	public int insertMobileType(MobileTypeBean type){
		final String sql = "insert into mobile_type(mobileTypeName,mobileWidth,mobileHeight,softwareVersion,companyId,createTime)" +
			"values(?,?,?,?,?,now())";
		try {
			return jdbcTemplate.update(sql, new Object[]{type.getMobileTypeName(),type.getMobileWidth(),type.getMobileHeight(),
					type.getSoftwareVersion(),type.getCompanyId()});
		} catch (DataAccessException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 根据公司ID查询机型数量
	 * @param companyId
	 * @return
	 */
	public int findCountByCompanyId(int companyId){
		final String sql = "select count(0) from mobile_type where companyId = ?";
		return jdbcTemplate.queryForInt(sql,companyId);
	}
	
	/**
	 * 根据公司ID分页查询手机型号
	 * @param companyId
	 * @param start
	 * @param pageSize
	 * @return
	 * @throws SQLException
	 */
	public List<MobileTypeBean> findMobileTypeByCompanyId(int companyId,int start,int pageSize) throws SQLException{
		final String sql = "select * from mobile_type where companyId = ? order by createTime desc limit ?,?";
		return queryRunner.query(sql, new BeanListHandler<MobileTypeBean>(MobileTypeBean.class), new Object[]{companyId,start,pageSize});
	}
	
	/**
	 * 根据公司ID查询手机型号
	 * @param companyId
	 * @param start
	 * @param pageSize
	 * @return
	 * @throws SQLException
	 */
	public List<MobileTypeBean> findMobileTypeByCompanyId(int companyId) throws SQLException{
		final String sql = "select * from mobile_type where companyId = ?";
		return queryRunner.query(sql, new BeanListHandler<MobileTypeBean>(MobileTypeBean.class), companyId);
	}
	
	/**
	 * 更新手机型号
	 * @param type
	 * @return
	 */
	public int updateMoileType(MobileTypeBean type){
		final String sql = "update mobile_type set mobileTypeName=?,mobileWidth=?,mobileHeight=?,softwareVersion=?,companyId=? where mobileTypeId=?";
		try {
			return jdbcTemplate.update(sql, new Object[]{type.getMobileTypeName(),type.getMobileWidth(),type.getMobileHeight(),
					type.getSoftwareVersion(),type.getCompanyId(),type.getMobileTypeId()});
		} catch (DataAccessException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 根据ID获取手机型号
	 * @param mobileTypeId
	 * @return
	 * @throws SQLException
	 */
	public MobileTypeBean findMobileTypeById(int mobileTypeId) throws SQLException{
		final String sql = "select * from mobile_type where mobileTypeId = ?";
		return queryRunner.query(sql, new BeanHandler<MobileTypeBean>(MobileTypeBean.class), mobileTypeId);
	}
	
	/**
	 * 删除手机型号
	 * @param mobileTypeId
	 * @return
	 * @throws SQLException
	 */
	public int delMobileTypeById(int mobileTypeId){
		final String sql = "delete from mobile_type where mobileTypeId = ?";
		return jdbcTemplate.update(sql, mobileTypeId);
	}
}
